package com.andnnl.vcode;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created on Mar 15, 2007
 * <p>Title: 产生验证码</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: 东软软件股份有限公司</p>
 * <p>Department: 软件开发事业部</p>
 * 
 * @author 王恺 wang.kai@neusoft.com
 * @version 1.0
 * @update 修改日期 修改描述
 */
public class GenerateCheckCode
{
    public static final String CHECK_CODE_KEY = "check_code";
    public static final int WIDTH = 64;
    public static final int HEIGHT = 20;
    
    /**
     * <p>取得验证码图片，并把生成的数字放到HttpSession中</p>
     * @return
     * @author:王恺
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public static BufferedImage getImage()
    {
        //创建内存图象并获得其图形上下文
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        //产生随机的认证码
        char[] rands = generateCheckCode();
        //产生图像
        drawBackground(g);
        drawRands(g, rands);
        //结束图像的绘制过程，完成图像
        g.dispose();
        //将当前验证码存入到Session中
//        session.setAttribute(CHECK_CODE_KEY, new String(rands));
        return image;
    }
    
    /**
     * <p>随机产生验证码字符</p>
     * @return
     * @author:王恺
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public static char[] generateCheckCode()
    {
        //定义验证码的字符表
        String chars = "23456789";
        char[] rands = new char[4];
        for (int i = 0; i < 4; i++)
        {
            int rand = (int) (Math.random() * chars.length());
            rands[i] = chars.charAt(rand);
        }
        return rands;
    }

    /**
     * <p>随机产生验证码字符</p>
     * @return
     * @author:pw
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public static Map<String,Object> generateCheckCodeMap()
    {
    	Map<String,Object> map = new HashMap<String,Object>();
    	 String[] rands = new String[4];
    	 Random random = new Random();
    	int a = random.nextInt(4) + 5;
        int b = random.nextInt(4);
        String randomCode = "";
        int validateCode = 0;
           //随机加减乘,除法要处理意外，忽略
        int c = random.nextInt(3);
        switch (c) {
          case 0:
        	   rands[0] =  a+"";
        	   rands[1] = "+";
        	   rands[2] =  b+"";
        	   rands[3] =  "=";
               validateCode = a + b;
               break;
          case 1:
        	  rands[0] =  a+"";
        	  rands[1] = "-";
        	  rands[2] =  b+"";
        	  rands[3] =  "=";
              validateCode = a - b;
               break;
          case 2:
        	  rands[0] =  a+"";
        	  rands[1] = "x";
        	  rands[2] =  b+"";
        	  rands[3] =  "=";
              validateCode = a * b;
               break;
          default:
        	  rands[0] =  a+"";
        	  rands[1] = "+";
        	  rands[2] =  b+"";
        	  rands[3] =  "=";
              validateCode = a + b;
              break;
         }
        map.put("rands", rands);
        map.put("validateCode", validateCode);
        return map;
    }
    
    /**
     * <p>字符位置、字体属性</p>
     * @param g
     * @param rands
     * @author:王恺
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public static void drawRands(Graphics g, char[] rands)
    {
        g.setColor(Color.BLACK);
        g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18));
        //在不同的高度上输出验证码的每个字符 
        g.drawString("" + rands[0], 1, 17);
        g.drawString("" + rands[1], 16, 15);
        g.drawString("" + rands[2], 31, 18);
        g.drawString("" + rands[3], 46, 16);
        //System.out.println(rands);
    }

    
    /**
     * <p>字符位置、字体属性</p>
     * @param g
     * @param rands
     * @author:pw
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public static void drawRands1(Graphics g, String[] rands)
    {
        g.setColor(Color.BLACK);
        g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18));
        //在不同的高度上输出验证码的每个字符 
        g.drawString(rands[0], 1, 17);
        g.drawString(rands[1], 16, 15);
        g.drawString(rands[2], 31, 18);
        g.drawString(rands[3], 46, 16);
        //System.out.println(rands);
    }
    
    /**
     * <p>背景</p>
     * @param g
     * @author:王恺
     * @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
     */
    public static void drawBackground(Graphics g)
    {
        //画背景
        g.setColor(new Color(0xDCDCDC));
        g.fillRect(0, 0, WIDTH, HEIGHT);
        //随机产生120个干扰点
        for (int i = 0; i < 120; i++)
        {
            int x = (int) (Math.random() * WIDTH);
            int y = (int) (Math.random() * HEIGHT);
            int red = (int) (Math.random() * 255);
            int green = (int) (Math.random() * 255);
            int blue = (int) (Math.random() * 255);
            g.setColor(new Color(red, green, blue));
            g.drawOval(x, y, 1, 0);
        }
        //画4条直线
        for (int i = 0; i < 4; i++)
        {
            int red = (int) (Math.random() * 255);
            int green = (int) (Math.random() * 255);
            int blue = (int) (Math.random() * 255);
            g.setColor(new Color(red, green, blue));
            g.drawLine((int) (Math.random() * WIDTH), (int) (Math.random() * HEIGHT), (int) (Math.random() * WIDTH), (int) (Math.random() * HEIGHT));
        }
    }
}